package com.yao.presenter;

import com.alibaba.fastjson.JSON;
import com.base.mvp.BasePresenter;
import com.yao.AppConfig;
import com.yao.http.Http;
import com.yao.http.Result;
import com.yao.presenter.bean.AppBean;
import com.yao.presenter.bean.BannerBean;
import com.yao.presenter.bean.IndexDataBean;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import org.reactivestreams.Publisher;

import java.util.HashMap;
import java.util.List;

public class IndexPresenter extends BasePresenter<IndexContract.View, IndexContract.Model> implements IndexContract.Presenter {

    @Override
    protected IndexContract.Model getModel() {
        return new IndexModel();
    }

    @Override
    public void getData(String apkVersion,String sysVersion) {
        if (isViewAttached()) {
            mView.showLoading();
            try {
                Flowable<Result<AppBean>> flowable = mModel.getAppUpdateInfo(apkVersion,sysVersion);
                mDisposable = flowable.flatMap((Function<Result<AppBean>, Publisher<Result<List<IndexDataBean>>>>) result -> {
                    mView.hideLoading();
                    if (result.getCode() == 1) {
                        mView.onSuccessUpdate(result.getData());
                    } else {
                        mView.onFail(result.getCode(), result.getMsg());
                    }
                    return mModel.getIndexData();
                }).flatMap((Function<Result<List<IndexDataBean>>, Publisher<Result<List<BannerBean>>>>) result -> {
                    mView.hideLoading();
                    if (result.getCode() == 1) {
                        mView.onSuccess(result.getData());
                    } else {
                        mView.onFail(result.getCode(), result.getMsg());
                    }
                    return mModel.getIndexBanner();
                }).subscribe(result -> {
                    mView.hideLoading();
                    if (result.getCode() == 1) {
                        mView.onSuccessBanner(result.getData());
                    } else {
                        mView.onFail(result.getCode(), result.getMsg());
                    }
                }, throwable -> {
                    mView.hideLoading();
                    mView.onFail(0, throwable.getMessage());
                });
            } catch (Exception e) {
                mView.hideLoading();
                mView.onError(e);
            }
        }
    }

    class IndexModel implements IndexContract.Model {

        @Override
        public IndexContract.Api getHttp() {
            return Http.getInstance().getRetrofit().create(IndexContract.Api.class);
        }

        @Override
        public void destroy() {
            Http.cancleAll(AppConfig.Http.API_INDEX_DATA);
            Http.cancleAll(AppConfig.Http.API_INDEX_BANNER);
            Http.cancleAll(AppConfig.Http.API_UPDATE);
        }

        @Override
        public Flowable<Result<List<IndexDataBean>>> getIndexData() {
            return getHttp().getIndexData().subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread());
        }

        @Override
        public Flowable<Result<List<BannerBean>>> getIndexBanner() {
            return getHttp().getIndexBanner().subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread());
        }

        @Override
        public Flowable<Result<AppBean>> getAppUpdateInfo(String apkVersion, String sysVersion) {
            HashMap<String, String> paramsMap = new HashMap<>();
            try {
                paramsMap.put("apkVersion", apkVersion);
                paramsMap.put("sysVersion", sysVersion);
            } catch (Exception e) {
                e.printStackTrace();
            }
            RequestBody body = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), JSON.toJSONString(paramsMap));
            return getHttp().getAppUpdateInfo(body).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread());
        }
    }
}
